﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4socket.config", Watch = true)]
namespace SocketControl.Log
{
    public class LogHelper
    {
        #region error
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        public static void WriteError(Type t, Exception ex)
        {
            LogCore("error", t, ex, "Error");
        }

        public static void WriteError(Type t, string methodName, Exception e)
        {
            LogCore("error", t, e, methodName);
        }

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>

        public static void WriteError(Type t, string msg)
        {
            LogCore("error", t, msg);
        }
        #endregion

        #region info
        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ex"></param>
        public static void WriteInfo(Type t, Exception ex)
        {
            LogCore("info", t, ex, "Info");
        }


        public static void WriteInfo(Type t, string methodName, Exception e)
        {
            LogCore("info", t, e, methodName);
        }

        /// <summary>
        /// 输出日志到Log4Net
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>

        public static void WriteInfo(Type t, string msg)
        {
            LogCore("info", t, msg);
        }
        #endregion
        private static void LogCore(string type, Type t, Exception e, string methodName = null)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            switch (type.ToLower())
            {
                case "error":
                    {
                        if (log != null) log.Error(message: methodName ?? "error", exception: e);
                        break;
                    }
                case "info":
                    {
                        if (log != null) log.Info(message: methodName ?? "info", exception: e);
                        break;
                    }
            }
        }

        private static void LogCore(string type, Type t, string e)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            switch (type.ToLower())
            {
                case "error":
                    {
                        if (log != null) log.Error(e);
                        break;
                    }
                case "info":
                    {
                        if (log != null) log.Info(e);
                        break;
                    }
            }
        }
    }
}
